package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.AddressDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.AddressMapper;
import com.iflytek.model.Address;
import com.iflytek.service.AddressService;
import com.iflytek.service.RoleService;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.AddressVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Resource
    private AddressMapper addressMapper;
    @Resource
    private AddressService addressService;

    @Resource
    private RoleService roleService;

    /**
     * @return
     */
    @Override
    public Result<List<AddressVO>> findAll() {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        List<Address> addressList = addressMapper.selectList(queryWrapper);
        List<AddressVO> addressVOList = CopyUtil.copyList(addressList, AddressVO.class);
        return Result.success(addressVOList);
    }

    /**
     * saveAddress
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveAddress(AddressDTO dto) {
        //Address address = new Address();
        if (dto == null) {
            return Result.error("1", "内容为空");
        }
        Address address = CopyUtil.copy(dto, Address.class);
        if (StringUtils.isEmpty(address.getId())) {
            if (StringUtils.isEmpty(address)) {
                return Result.error("1", "保存失败");
            }
//            address.setId(UuidUtil.getShortUuid());

        }
        addressMapper.insert(address);
        return Result.success(true);
    }

    /**
     * updateAddress
     * @param dto
     * @return
     */
    @Override
    public Result<Boolean> updateAddress(AddressDTO dto) {
        Address address = new Address();
        BeanUtils.copyProperties(dto, address);
        int count = addressMapper.updateById(address);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("1", "更新失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Result<AddressVO> findById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        Address address = addressMapper.selectById(id);
        AddressVO addressVO = CopyUtil.copy(address, AddressVO.class);

        return Result.success(addressVO);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public Result<AddressVO> deleteById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        int i = addressMapper.deleteById(id);
        if (i > 0) {
            Result.success();
        } else {

            Result.error("204", "错误");
        }
        return Result.success();
    }

    /**
     * 分页
     *
     * @param dto
     * @return
     */
    @Override
    public Result<PageVO<AddressVO>> findPage(PageDTO<AddressDTO> dto) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper();

        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        //分页
        PageHelper.startPage(dto.getPageNum(), dto.getSize());

        List<Address> addresss = addressMapper.selectList(queryWrapper);

        List<AddressVO> addressVOList = new ArrayList<>();
        addressVOList = CopyUtil.copyList(addresss, AddressVO.class);

        PageInfo<Address> addressPageInfo = new PageInfo<>(addresss);
        PageVO<AddressVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(addressPageInfo.getTotal());
        pageVO.setRecords(addressVOList);
        return Result.success(pageVO);
    }

}
